import * as Cesium from '@cesiumjs';

/* 162 */
/***/

var xbsjEarthScriptUrl = void 0;

function createCameraModel(viewer) {
  xbsjEarthScriptUrl = xbsjEarthScriptUrl || _HTML2.default.getScriptBaseUrl('XbsjEarth');
  var model = viewer.scene.primitives.add(Cesium.Model.fromGltf({
    url: xbsjEarthScriptUrl + 'models/camera1/camera1.gltf?k=path',
    allowPicking: false,
    // cacheKey: Cesium.createGuid(),
    forwardAxis: Cesium.Axis.X
  }));

  // model.opaquePass = Cesium.Pass.TRANSLUCENT;
  model.opaquePass = Cesium.Pass.OVERLAY;
  model.cull = false;
  model.shadows = Cesium.ShadowMode.DISABLED;
  model.luminanceAtZenith = 1.2;
  // model.xbsjForceDepthTestDisabled = true;
  // model.colorBlendMode = Cesium.ColorBlendMode.MIX;
  // model.xbsjForceDepthTestDisabled = true;
  // model.opaquePass = Cesium.Pass.OVERLAY;
  // model.modelMatrix = this._lastMat4;

  model.minimumPixelSize = 50;

  return model;
}

function initCameraHelpers(earth) {
  if (!earth._cameraModelsPool) {
    earth._cameraModelsPool = [];

    earth._createCameraModel = function() {
      if (earth._cameraModelsPool.length > 0) {
        return earth._cameraModelsPool.pop();
      } else {
        return createCameraModel(earth);
      }
    };

    earth._destroyCameraModel = function(cameraHelper) {
      earth._cameraModelsPool.push(cameraHelper);
    };

    earth.disposers.push(function() {
      earth._cameraModelsPool.forEach(function(e) {
        earth.czm.scene.primitives.remove(e);
      });
      earth._cameraModelsPool.length = 0;
    });
  }
}

/**
 * 内部使用，不对外
 * @memberof Obj
 * @class
 * @extends XbsjCzmObj
 * @ignore
 */

var CameraHelper = function(_XbsjCzmObj) {
  (0, _inherits3.default)(CameraHelper, _XbsjCzmObj);

  function CameraHelper(earth, guid) {
    (0, _classCallCheck3.default)(this, CameraHelper);

    var _this = (0, _possibleConstructorReturn3.default)(this, (CameraHelper.__proto__ || (0, _getPrototypeOf2
      .default)(CameraHelper)).call(this, earth, guid));

    initCameraHelpers(earth);

    _this.disposers.push(_XbsjBase2.default.registerPosition(_this));
    _this.disposers.push(_XbsjBase2.default.registerRotation(_this));

    _this.disposers.push((0, _mobx.reaction)(function() {
      return {
        position: [].concat((0, _toConsumableArray3.default)(_this.position)),
        rotation: [].concat((0, _toConsumableArray3.default)(_this.rotation))
      };
    }, function() {
      _this._updateCameraPostionAndRotation();
    }));

    _this._cameraModel = undefined;

    var updateShow = function updateShow() {
      var show = _this.show;
      if (show && !_this._cameraModel) {
        _this._cameraModel = earth._createCameraModel();
        _this._updateCameraPostionAndRotation();
        _this._cameraModel.show = true;
      } else if (!show && _this._cameraModel) {
        _this._cameraModel.show = false;
        earth._destroyCameraModel(_this._cameraModel);
        _this._cameraModel = undefined;
      }
    };

    _this.disposers.push((0, _mobx.autorun)(updateShow));

    _this.disposers.push(function() {
      _this.show = false;
      updateShow();
    });
    return _this;
  }

  (0, _createClass3.default)(CameraHelper, [{
    key: '_updateCameraPostionAndRotation',
    value: function _updateCameraPostionAndRotation() {
      if (this._cameraModel) {
        var _Cesium$Cartesian;

        var hpr = new(Function.prototype.bind.apply(Cesium.HeadingPitchRoll, [null].concat((0,
          _toConsumableArray3.default)(this.rotation))))();
        hpr.heading -= Cesium.Math.PI_OVER_TWO;
        var center = (_Cesium$Cartesian = Cesium.Cartesian3).fromRadians.apply(_Cesium$Cartesian, (0,
          _toConsumableArray3.default)(this.position).concat([undefined, new Cesium.Cartesian3()]));
        var hprToFF = Cesium.Transforms.headingPitchRollToFixedFrame;
        hprToFF(center, hpr, undefined, undefined, this._cameraModel.modelMatrix);
      }
    }
  }, {
    key: 'isCameraLike',
    get: function get() {
      return true;
    }
  }]);
  return CameraHelper;
}(_XbsjCzmObj3.default);

CameraHelper.defaultOptions = {
  position: [0, 0, 0],
  rotation: [0, 0, 0],
  show: false
};

_XbsjObject2.default.registerType(CameraHelper, 'CameraHelper');

export default CameraHelper;
